
local DB = require("common.DB")
local LuaMongo = _G.lua_mongo
local Util = require("common.Util")
local FriendLogic = require("friend.FriendLogic")
local FriendDefine = require("friend.FriendDefine")
local Human = require("core.Human")
local DBOpAnd = {uuid1=nil,uuid2=nil}											-- and 
local DBOpOr = {["$or"] = {{uuid1=""},{uuid2=""}}}								-- or 
local DBOpRemove = {_id=nil}													-- remove
local DBQuery = {uuid = nil,type=nil}											-- 单查询
local DBUpdate = {_id = nil}
--local DBQuery1 = {uuid=nil,["$orderby"]={ time = -1 }}
local DBQuery1 = {uuid=nil,type=nil}
local DBQuery2 = {_id = nil}

function isFriend(uuid1,uuid2)
	if uuid1 > uuid2 then
		uuid1,uuid2 = uuid2,uuid1
	end
	DBOpAnd.uuid1 = uuid1
	DBOpAnd.uuid2 = uuid2
	local data = {}
	LuaMongo.find(DB.db_friend, DBOpAnd)
	if not LuaMongo.next(data) then
		return 
	end
	return true,data
end

function saveFriend(data)
	if not data._id then
		assert()
	end
	DBUpdate._id = data._id
	LuaMongo.update(DB.db_friend, DBUpdate, data)
end

function getFriendCnt(uuid)
	local data = getFriends(uuid)
	if not data then
		return 0
	end
	return getLen(data)
end

function getLen(table)
	local len = 0
	for k,v in pairs(table) do 
		if v then
			len = len + 1
		end
	end
	return len
end

function addFriendData(uuid1,uuid2,createTime)
	if isFriend(uuid1,uuid2) then
		return
	end
	local db = wrapperFriendData(uuid1,uuid2,createTime)
	LuaMongo.insert(DB.db_friend, db)
	return true,db
end

function wrapperFriendData(uuid1,uuid2,createTime)
	if uuid1 > uuid2 then
		uuid1,uuid2 = uuid2,uuid1
	end
	-- qimi ; update_daily_time ;dailySitQinmi
	return {uuid1 = uuid1,uuid2 = uuid2,createTime = createTime}
end

function delFriendData(uuid1,uuid2)
	local pass,data = isFriend(uuid1,uuid2)
	if not pass then
		return
	end
	DBOpRemove._id = data._id
	LuaMongo.remove(DB.db_friend,DBOpRemove)
end

function getFriends(uuid)
	DBOpOr["$or"][1].uuid1 = uuid
	DBOpOr["$or"][2].uuid2 = uuid
	LuaMongo.find(DB.db_friend, DBOpOr)
	local data = {}
	while true do
		local tmp = {}
		if not LuaMongo.next(tmp) then
			break
		end
		if tmp.uuid1 ~= uuid then
			data[tmp.uuid1] = tmp
		elseif tmp.uuid2 ~= uuid then
			data[tmp.uuid2] = tmp
		else
			assert()
		end
	end
	return data
end

function addFriendActivityComment(ownerUuid,activityUuid,content)
	local pass,data = isActivityExistInDB(activityUuid)
	if not pass then
		return
	end
	local now = os.time()
	local comment = {}
	comment.ownerUuid = ownerUuid
	comment.content = content
	comment.time = now
	table.insert(data.comment, 1 ,comment)
	if #data.comment > FriendDefine.COMMONT_COUNT then
		data.comment[#data.comment] = nil	
	end
	data.updateTime = now
	DBUpdate._id = data._id
	LuaMongo.update(DB.db_friend_activity, DBUpdate, data)
	return data
	
end

-- 增加好友动态: add friend
function addActByFriend(uuid,targetUuid) 
	DBQuery.uuid = uuid
	DBQuery.type = FriendDefine.FRIEND_ACTIVITY_ADD_FRIEND
	LuaMongo.find(DB.db_friend_activity, DBQuery)
	local data = {}
	local time = os.time()
	if not LuaMongo.next(data) then
		data = {uuid = uuid,type = FriendDefine.FRIEND_ACTIVITY_ADD_FRIEND,time = time,updateTime = time,friend={[1]=targetUuid},comment = {}}
		LuaMongo.insert(DB.db_friend_activity, data)
	else
		
		data.updateTime = time
		if not data.friend[1] or data.friend[1] ~= targetUuid then
			table.insert(data.friend, 1 ,targetUuid)
		end
		
		if data.friend[3] then
			data.friend[3] = nil
		end
		DBUpdate._id = data._id
		LuaMongo.update(DB.db_friend_activity, DBUpdate, data)
		
	end
	return data
end

-- 增加好友动态: get item
function addActByItem(uuid,grid,mapID)
	if not grid then
		assert()
	end
	local newGrid
	local time = os.time()
	newGrid = {}
	Util.copyTable(grid,newGrid)
	
	local data = {uuid = uuid,type = FriendDefine.FRIEND_ACTIVITY_GET_ITEM,time = time,updateTime = time,grid = newGrid,mapID = mapID,comment = {}}
	LuaMongo.insert(DB.db_friend_activity, data)
	return data
end

-- 增加好友动态: change sign
function addActBySign(uuid,sign) 
	if not sign then
		assert()
	end
	local time = os.time()
	local data = {uuid = uuid,type = FriendDefine.FRIEND_ACTIVITY_STRING,time = time,updateTime = time,sign = sign,comment = {}}
	LuaMongo.insert(DB.db_friend_activity, data)
	return data
end

--增加好友动态：lv
function addActByLv(uuid,lv)
	if not lv then
		assert()
	end
	local time = os.time()
	local data = {uuid = uuid,type = FriendDefine.FRIEND_ACTIVITY_LV,time = time,updateTime = time,lv = lv ,comment = {}}
	LuaMongo.insert(DB.db_friend_activity, data)
	return data
end

-- 增加好友动态：rank
function addActByRank(uuid,name,rank)
	if not rank then
		assert()
	end
	local time = os.time()
	local data = {uuid = uuid,type = FriendDefine.FRIEND_ACTIVITY_RANK,time = time,updateTime = time,name = name, rank = rank,comment = {}}
	LuaMongo.insert(DB.db_friend_activity, data)
	return data
end

-- 增加好友动态：equip强化
function addActByEquipStrengthen(uuid,grid)
	if not grid then
		assert()
	end
	
	newGrid = {}
	Util.copyTable(grid,newGrid)
	
	local time = os.time()
	local data = {uuid = uuid,type = FriendDefine.FRIEND_ACTIVITY_EQUIP_STRENGTHEN,time = time,updateTime = time,grid = newGrid,comment = {}}
	LuaMongo.insert(DB.db_friend_activity, data)
	return data
end

-- 增加好友动态：境界提长
function addActByJingjie(uuid,jingjieID)
	if not jingjieID then
		assert()
	end
	local time = os.time()
	local data = {uuid = uuid,type = FriendDefine.FRIEND_ACTIVITY_JINGJIE,time = time,updateTime = time,jingjieID = jingjieID,comment = {}}
	LuaMongo.insert(DB.db_friend_activity, data)
	return data
end

function getActivitys(human)

	local query = {} 
	query["$or"] = {} 
	local len = 0
	for k in pairs(human.friend) do 
		len = len + 1
		query["$or"][len] = {uuid = k}		
	end
	
	len = len + 1
	query["$or"][len] = {uuid = human.db._id,type = {["$ne"]=FriendDefine.FRIEND_ACTIVITY_ADD_FRIEND}}
	
	if len == 0 then
		return nil
	end
	
	LuaMongo.find(DB.db_friend_activity, {["$query"]=query,["$orderby"]={time=-1}} ,nil,50) 
	
	local data = {}
	len = 0
	while true do
		local tmp = {}
		if not LuaMongo.next(tmp) then
			break
		end

		len = len + 1
		data[len] = tmp	
	end
		
	return data
end

function getActivity(activityUuid)
	DBQuery2._id = activityUuid
	LuaMongo.find(DB.db_friend_activity,DBQuery2) 
	local data = {}
	if not LuaMongo.next(data) then
		return 
	end
	return data
end

function getActivitysByType(uuid,type)
	DBQuery1.uuid = uuid
	DBQuery1.type = type
	LuaMongo.find(DB.db_friend_activity, {["$query"]=DBQuery1,["$orderby"]={time=-1}} ,nil,FriendDefine.FRIEND_ACTIVITY_CNT_LIMIT[type]) 
	local data = {}
	local len = 0
	while true do 
		local tmp = {}
		if not LuaMongo.next(tmp) then
			break
		end
		len = len + 1
		data[len] = tmp	
	end
	return data
end

function delActivity(activityUuid)
	DBOpRemove._id = activityUuid
	LuaMongo.remove(DB.db_friend_activity,DBOpRemove)
end

function onUserLogin(human)
	local humanUuid = human._id
	human.friend ={} 
	local data = getFriends(humanUuid)
	if not data then
		return
	end
	local target = nil
	local onlineUuid = Human.onlineUuid
		
	-- 好友 上线通知
	for k, v in pairs(data) do 
		human.friend[k] = v
		target = onlineUuid[k]
		if target then
			FriendLogic.sendOnLineNotify(human,target.fd,v,FriendDefine.FRIEND)
		end
	end	
	-- 离线好友请求
--	FriendLogic.offLineFriendRequset(human)
	FriendLogic.sendFriendList(human)
end

function onUserLoginOut(human)
	local target = nil
	local onlineUuid = Human.onlineUuid
	if not human.friend then return end
	for k,v in pairs(human.friend) do
		target = onlineUuid[k]
		if target then
			FriendLogic.sendOffLineNotify(human,target.fd,v)
		end
	end
	FriendLogic.clearFriendRequset(human)
end

function isActivityExistInDB(uuid)
	DBQuery2._id = uuid
	LuaMongo.find(DB.db_friend_activity, DBQuery2)
	local data = {}
	if not LuaMongo.next(data) then
		return false
	end
	return true,data
end
